package com.pzhu.record.controller;

import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Reward;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author Zhou YuSen
 * @date 2020/4/12 23:42
 */
@Controller
@RequestMapping("/reward")
public class RewardController extends BaseAction {

    @RequestMapping("/insert")
    public String insert(Reward reward, Model model, MultipartFile pic,
                         HttpServletRequest request,
                         @RequestParam(value = "basicinformationId",required = true) String basicinformationId) throws IOException {
            reward.setBasicinformationId(basicinformationId);
        int parseInt = Integer.parseInt(reward.getRewardCheck());
        if (1 == parseInt) {
            reward.setRewardCheck("通过");
        } else if (0 == parseInt) {
            reward.setRewardCheck("不通过");
        }
        String filename = pic.getOriginalFilename();
        if (filename != null && filename.length() > 0) {
            String path = request.getServletContext().getRealPath("/upload");
            String imgName = path + "\\" + filename;
            File file = new File(imgName);
            pic.transferTo(file);
            reward.setRewardPhoto(imgName);
        }

        int insert = rewardService.insertSelective(reward);
        if (insert != 1) {
            model.addAttribute("message", "添加失败");
        } else {
            model.addAttribute("message", "添加成功");
        }
        return "nav";
    }



}
